<?php
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class AccountAgentTotalDao extends BaseDao {

	protected $_name;
	protected $joinAgentTradeLog;

	public function _setup(){
		$this->_name="account_agent_totals";
		$this->joinAgentTradeLog ="account_agent_trade_logs";
		parent::_setup();
	}

//	public function getAgentById($id){
//		$select=$this->db_r->select();
//		$select->from($this->_name);
//		$select->where($this->_name . '.id=?',$id);
//		$select->where($this->_name . '.is_delete=?',0);
//		$sql=$select->__toString();
//		return $this->db_r->fetchRow($sql);
//	}
//
	public function updateAgent($set,$id){
		$where=$this->db_w->quoteInto('id=?',$id);
		$this->updateData($set, $where);
	}
//
//	public function deleteAgent($id){
//		$set=array('is_delete'=>1);
//		$where=$this->db_w->quoteInto('id=?',$id);
//		$this->updateData($set,$where);
//	}
//
	public function getAgentTotalByTradeLogId($tradeLogId){
		$select=$this->db_r->select();
		$select->from($this->_name,array('*'));
		$select->joinLeft($this->joinAgentTradeLog,$this->_name . ".user_id=$this->joinAgentTradeLog.user_id",array("user_id"));
		$select->where($this->joinAgentTradeLog . '.id=?',$tradeLogId);
		$select->where($this->_name . '.is_delete=?',0);
		$sql=$select->__toString();
		return $this->db_r->fetchRow($sql);
	}

	public function getInfoByUserId($userId){
		$select=$this->db_r->select();
		$select->from($this->_name);
		$select->where($this->_name . '.user_id=?',$userId);
		$select->where($this->_name . '.is_delete=?',0);
		$sql=$select->__toString();
		return $this->db_r->fetchRow($sql);
	}

//	public function getAccountAgentTotalsByUserId($id){//david
//
//		$select=$this->db_r->select();
//		$select->from($this->_name);
//		$select->where($this->_name . '.user_id=?',$id);
//		$select->where($this->_name . '.is_delete=?',0);
//		$sql=$select->__toString();
//		return $this->db_r->fetchRow($sql);
//	}

	public function updateAccountAgentTotalsByUserId($data, $id){//david
		$where=$this->db_w->quoteInto('user_id=?',$id);
		$this->updateData($data, $where ,true);
	}
//	public function delAgentTotalByUserid($userid){//jsaon
//
//		$set=array('is_delete'=>1);
//		$where=$this->db_w->quoteInto('user_id=?',$userid);
//		$this->db_w->update($this->_name,$set,$where);
//	}
//	public function getAgentTotalInfoByUserid($userid){//jsaon
//
//		$select=$this->db_r->select();
//		$select->from($this->_name,array('user_id as userId','balance','debt_balance as debtBalance','debt_total as debtTotal','usage_balance as usageBalance','freezed_balance as freezedBalance','supplement_cnt as supplymentCnt','supplement_sum as supplymentSum','supplement_last_time as supplymentLastTime','order_cnt as orderCnt','order_sum as orderSum','order_self_sale_sum as orderSelfSaleSum','order_auto_recv_sum as orderAutoRecvSum','order_campus_sum as orderCampusSum','order_last_time as orderLastTime','order_last_type as orderLastType','order_dealing_cnt as orderDealingCnt','order_dealing_sum as orderDealingSum','withdraw_cnt as withdrawCnt','withdraw_sum as withdrawSum','withdraw_last_time as withdrawLastTime','profit'));
//		$select->where($this->_name . '.user_id=?',$userid);
//		$select->where($this->_name . '.is_delete=?',0);
//		$sql=$select->__toString();
//		return $this->db_r->fetchRow($sql);
//	}
//	public function getBestTrade($key){
//		 $select=$this->db_r->select();
//		$select->from($this->_name,'*');
//		$select->join('agents','agents.user_id = account_agent_totals.user_id',array('name'));
//		$select->where($this->_name .'.is_delete=?',0);
//		$select->where('agents.is_delete=?',0);
//		$select->order($key.' desc');
//		$sql=$select->__toString();
//		return $this->db_r->fetchAll($sql);
//	}
}
?>